//
//  CatTimingListCell.m
//  DoorBell
//
//  Created by Chun Hung Cheung on 2023/3/18.
//  Copyright © 2023 Chun Hung Cheung. All rights reserved.
//

#import "CatTimingListCell.h"


@interface CatTimingListCell()
@property(nonatomic,strong)UIView *contrainView;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UILabel *weekLabel;
@property(nonatomic,strong)UISwitch *switchButton;

@property (nonatomic, weak) UISwipeGestureRecognizer *rightSwipe; //向右清扫手势
@property (nonatomic,strong)UIButton *deleteBtn;

@end

@implementation CatTimingListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


- (UIView*)contrainView
{
    if(!_contrainView){
        _contrainView = [[UIView alloc]init];
        _contrainView.layer.cornerRadius = 8;
        _contrainView.layer.borderWidth = 0.5;
        _contrainView.layer.borderColor = [HexChangeRGB colorWithHexString:@"#E1E1E1"].CGColor;
        _contrainView.layer.masksToBounds = YES;
       [self.contentView addSubview:_contrainView];
       [_contrainView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(14);
            make.right.equalTo(self.contentView.mas_right).offset(-14);
            make.top.equalTo(self.contentView.mas_top);
            make.bottom.equalTo(self.contentView.mas_bottom);
       }];
        
       //3、给容器containerView绑定左右滑动清扫手势
       UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
       leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; //设置向左清扫
       [_contrainView addGestureRecognizer:leftSwipe];
       
       UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
       rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;//设置向右清扫
       [_contrainView addGestureRecognizer:rightSwipe];
        self.rightSwipe = rightSwipe;
       
        self.selectionStyle = UITableViewCellSelectionStyleNone; //设置单元格选中样式
       [self.contentView bringSubviewToFront:_contrainView];
        
    }
    return _contrainView;
}

//左滑动和右滑动手势
- (void)swipe:(UISwipeGestureRecognizer *)sender
{
    
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft){
        if (self.isOpenLeft) return; //已经打开左滑，不再执行
        
        //开始左滑： 先调用block关闭其他可能左滑的cell
        if (self.closeOtherCellSwipe){
            self.closeOtherCellSwipe();
        }
        
        [UIView animateWithDuration:0.25 animations:^{
            
            [self.contrainView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView.mas_left).offset(-74);
                make.width.mas_equalTo(kScreenWidth-28);
                make.bottom.equalTo(self.contentView.mas_bottom);
                make.top.equalTo(self.contentView.mas_top);
            }];
   
            [self.deleteBtn mas_remakeConstraints:^(MASConstraintMaker *make){
                 make.width.mas_equalTo(100);
                 make.right.equalTo(self.contentView.mas_right);
                 make.height.mas_equalTo(80);
                 make.centerY.equalTo(self.contentView.mas_centerY);
            }];
         
            [self.contentView layoutIfNeeded];

        }];
        self.isOpenLeft = YES;
    }
    else if (sender.direction == UISwipeGestureRecognizerDirectionRight){
        [self closeLeftSwipe]; //关闭左滑
    }
}

//关闭左滑，恢复原状
- (void)closeLeftSwipe{
    if (!self.isOpenLeft) return; //还未打开左滑，不需要执行右滑
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.contrainView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(14);
            make.width.mas_equalTo(kScreenWidth-28);
            make.bottom.equalTo(self.contentView.mas_bottom);
            make.top.equalTo(self.contentView.mas_top);
        }];

        [self.deleteBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.width.mas_equalTo(60);
             make.right.equalTo(self.contentView.mas_right).offset(60);
             make.height.mas_equalTo(80);
             make.centerY.equalTo(self.contentView.mas_centerY);
        }];
        
        [self.contentView layoutIfNeeded];
         
    }];
    self.isOpenLeft = NO;
}




- (UILabel*)timeLabel
{
    if (!_timeLabel) {
         _timeLabel = [[UILabel alloc]init];
        [self.contrainView addSubview:_timeLabel];
         _timeLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
         _timeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:24.0];
         _timeLabel.numberOfLines = 0;
         _timeLabel.textAlignment = NSTextAlignmentLeft;
        [_timeLabel sizeToFit];
        [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contrainView.mas_left).offset(14);
            make.top.equalTo(self.contrainView.mas_top).offset(12);
        }];
   }
    return _timeLabel;
}

- (UILabel*)weekLabel
{
     if (!_weekLabel) {
         _weekLabel = [[UILabel alloc]init];
        [self.contrainView addSubview:_weekLabel];
         _weekLabel.textColor = [HexChangeRGB colorWithHexString:@"#666666"];
         _weekLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14.0];
         _weekLabel.numberOfLines = 0;
         _weekLabel.textAlignment = NSTextAlignmentLeft;
        [_weekLabel sizeToFit];
        [_weekLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contrainView.mas_left).offset(12);
            make.top.equalTo(self.timeLabel.mas_bottom).offset(2);
        }];
   }
    return _weekLabel;
}


- (UISwitch*)switchButton
{
     if (!_switchButton) {
         _switchButton = [[UISwitch alloc]init];
         //_switchButton.transform = CGAffineTransformMakeScale(0.8, 0.8);
         _switchButton.onTintColor = [UIColor orangeColor];
        [_switchButton addTarget:self action:@selector(switchButtonClick:) forControlEvents:UIControlEventValueChanged];
        [self.contrainView addSubview:_switchButton];
        [_switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerY.equalTo(self.contrainView.mas_centerY);
           make.right.equalTo(self.contrainView.mas_right).offset(-14);
        }];
    }
    return _switchButton;
}

- (void)switchButtonClick:(UISwitch*)sender
{
    
    /*
    if ([self.list.is_share intValue] == 1) {
         sender.on = _model.status;
        [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"The invitee does not have permission to change the settings",nil) duration:2];
        return;
    }
    */
    
    if(self.timingStatusBlock){
       self.timingStatusBlock(sender.isOn,self.model);
    }
    
    
}

-(void)setModel:(CatTimingListModel *)model
{
    _model = model;
    self.timeLabel.text  = _model.time;
    self.weekLabel.text  = _model.weak;
    self.switchButton.on = _model.status;
    self.deleteBtn.backgroundColor = [UIColor redColor];
}


- (UIButton*)deleteBtn
{
    if (!_deleteBtn) {
         _deleteBtn = [[UIButton alloc] init];
        [self.contentView addSubview:_deleteBtn];
        [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(60);
            make.left.equalTo(self.contentView.mas_right).offset(60);
            make.height.mas_equalTo(80);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
        [_deleteBtn setTitle:NSLocalizedString(@"Delete",nil) forState:UIControlStateNormal];
        [_deleteBtn setBackgroundColor:[UIColor orangeColor]];
        //绑定打电话事件
        [_deleteBtn addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    }
     return _deleteBtn;
}


#pragma  mark
- (void)delete:(UIButton *)sender{
    //如果实现了删除block回调，则调用block
    if (self.deleteBlockMember){
        self.deleteBlockMember(self.model);
    }

}


@end
